package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.S;
import com.example.demo.entity.Category;
import com.example.demo.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//分类管理
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    //新增分类
    @PostMapping
    public S<String> save(Category category){
        categoryService.save(category);
        return S.success("成功");
    }
    //分页查询
    @GetMapping("/page")
    public S<Page> page(int page,int pageSize){
        Page<Category> page1=new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        categoryService.page(page1,lambdaQueryWrapper);
        return S.success(page1);
    }
    @DeleteMapping
    public S<String> delete(Long ids){
        log.info("删除分类"+ids);
        categoryService.delete(ids);
        return S.success("分类信息删除成功");
    }
    @PutMapping
    public S<String> update(Category category){
        log.info("修改分类"+category);
        categoryService.updateById(category);
        return S.success("修改成功");
    }
    //根据条件查询分类数据
    @GetMapping("/list")
    public S<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return S.success(list);
    }

}
